﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace WebApplication1
{
    public partial class userlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
             BindUsers();
        }


        public void BindUsers()
        {
            UserManagement us = new UserManagement();

            List<fos_User> allUsers = us.GetAllUsers(); ;

            lsvUsers.DataSource = allUsers;
            lsvUsers.DataBind();

        }

        protected void lsvUsers_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            //set current page startindex, max rows and rebind to false
            dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

            //rebind List View
            BindUsers();
        }


        public string CheckRole(object roleId)
        {
              if (roleId.ToString() == "1")
                return "Admin";
              else if (roleId.ToString() == "2")
                  return "Employee";
              else 
                  return "Caterer";



        }

        protected void lsvUsers_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lsvUsers.EditIndex = e.NewEditIndex;
            BindUsers();
        }

        protected void lsvUsers_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lsvUsers.EditIndex = -1;
            BindUsers();
        }

        protected void btnAddRole_Click(object sender, ImageClickEventArgs e)
        {
             ImageButton Btn = sender as ImageButton;
             Business.User userobj = (User)Session["User"];
             long userId = Convert.ToInt64(Btn.CommandArgument.ToString());
             DropDownList ddlRole =(DropDownList) lsvUsers.EditItem.FindControl("ddlRole");
             string selectedRoleId = ddlRole.SelectedValue;
             UserManagement us = new UserManagement();
             fos_User userobject = us.GetUser(userId);
             try
             {
                 us.AssociateRoleUserList(userobject, selectedRoleId, userobj.email);
                 // Back to itemtemplate
                 lsvUsers.EditIndex = -1;
                 BindUsers();
                 divMessage.Visible = true;
                 divMessage.CssClass = "alert alert-success  alert-dismissible";
                 lblMessage.Text = "New role added!";
             }
            catch (Exception ex)
             {
                 divMessage.Visible = true;
                 divMessage.CssClass = "alert alert-danger  alert-dismissible";
                 lblMessage.Text = ex.Message;
             }


        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            LinkButton Btn = sender as LinkButton;
            long userlinkRoleId = Convert.ToInt64(Btn.CommandArgument.ToString());
            UserManagement us = new UserManagement();

            Business.User userobj = (User)Session["User"];

            try
            {
                us.DeleteUserRole(userlinkRoleId, userobj.email);
                lsvUsers.EditIndex = -1;
                BindUsers();
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Role deleted!";
            }
            catch (Exception ex)
            {
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = ex.Message;
            }
        }
    }
}